﻿
//Author: 张亚涛
//createtime:2015-12-19 23:32:31
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "MemberPark" 表的方法;
    /// </summary>
    public class MemberParkBase
    {
        const string SQL_Select = "select [ID],[OpenID],[CommuntyID],[CommuntyName],[ParkID],[ParkName] from [MemberPark] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回MemberPark表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<MemberPark> GetAllMemberPark()
        {
            List<MemberPark> memberParkList = new List<MemberPark>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                MemberPark memberPark = new MemberPark(reader);
                memberParkList.Add(memberPark);
            }
            reader.Close();
            return memberParkList;
        }
        /// <summary>
        /// 根据MemberPark表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static MemberPark GetMemberParkById(long id)
        {
            MemberPark memberPark = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                memberPark = new MemberPark(reader);
            }
            reader.Close();
            return memberPark;
        }
        /// <summary>
        /// 根据会员微信ID字段返回会员停车位信息
        /// </summary>
        /// <param name="OpenID">微信ID</param>
        /// <returns></returns>        
        public static MemberPark GetMemberParkByOpenID(string OpenID)
        {
            MemberPark memberPark = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [OpenID] = @OpenID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@OpenID",OpenID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                memberPark = new MemberPark(reader);
            }
            reader.Close();
            return memberPark;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据MemberPark表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteMemberParkById(long id)
        {
            string sql = "delete from [MemberPark] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(MemberPark memberPark)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",memberPark.Id),
            new SqlParameter("@OpenID",memberPark.OpenID),
            new SqlParameter("@CommuntyID",memberPark.CommuntyID),
            new SqlParameter("@CommuntyName",memberPark.CommuntyName),
            new SqlParameter("@ParkID",memberPark.ParkID),
            new SqlParameter("@ParkName",memberPark.ParkName)
            };
            return paras;
        }
        /// <summary>
        /// 根据MemberPark表的ID字段更新数据 
        /// </summary> 
        /// <param name="MemberPark">memberPark</param>
        /// <returns></returns>       
        public static int UpdateMemberParkById(MemberPark memberPark)
        {
            string sql = "update [MemberPark] set [OpenID] = @OpenID,[CommuntyID] = @CommuntyID,[CommuntyName] = @CommuntyName,[ParkID] = @ParkID,[ParkName] = @ParkName where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(memberPark);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向MemberPark表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="memberPark">MemberPark</param>       
        /// <returns></returns>        
        public static int AddMemberPark(MemberPark memberPark)
        {
            string sql = "insert into [MemberPark]([OpenID],[CommuntyID],[CommuntyName],[ParkID],[ParkName])values(@OpenID,@CommuntyID,@CommuntyName,@ParkID,@ParkName)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(memberPark);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回MemberPark表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllMemberPark_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}